package com.zhao.controller;

import com.zhao.dao.EbickDao;
import com.zhao.pojo.*;
import com.zhao.service.DrivingLicenseService;
import com.zhao.service.EbickService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;


@Controller
public class EbickController {

    @Autowired
    EbickService ebickService;

    @Autowired
    EbickDao ebickDao;

    @Autowired
    DrivingLicenseService drivingLicenseService;

    /////////////////////// admin  ////////////////////////////////

    @ResponseBody
    @RequestMapping("admin-seeOneEbick.do")
    public String adminSeeOneEbick(String ebickID,HttpSession session){

        Admin admin = (Admin)session.getAttribute("admin");
        if(admin == null){
            return "redirect:/adminLogin";
        }
        String res = ebickService.adminSeeOneEbick(admin,ebickID,session);
        System.out.println(res);
        return res;
    }

    @RequestMapping("admin-getAllEbicks.do")
    public String adminGetAllEbicks(HttpSession session){

        Admin admin = (Admin)session.getAttribute("admin");
        if(admin == null){
            return "redirect:/adminLogin";
        }
        String res = ebickService.adminGetAllEbick(admin,session);
        if(!res.equals("success")){
            return "404";
        }
        return "redirect:/admin-allEbicks";
    }


    ////////////////////// 学生 ////////////////////////////////
    //我的全部电动车
    @RequestMapping("getAllMyEbicks.do")
    public String getAllMyEbicks(HttpSession session){
        Student student =(Student) session.getAttribute("student");
        if(student==null)return "redirect:/";
        String res = ebickService.findAllMyEbick(session);
        System.out.println(res);
        if(res.equals("请先登录")){
            return "redirect:/";
        }
        else if(!res.equals("success")){
            return "404";
        }

        return "newShowMyEbick";
    }

    @RequestMapping("ebickRegist.do")
    public String ebickRegist(@RequestParam("brand")String brand,
                              @RequestParam("buyfrom")String buyfrom,
                              @RequestParam("Details")String details,
                              @RequestParam("image") MultipartFile file,
                              HttpSession session){

        /*System.out.println("ebickRegistController");
        System.out.println(file.getOriginalFilename());
        System.out.println(file.getSize());*/
        Student student =(Student) session.getAttribute("student");
        if(student==null)return "redirect:/";
        Ebick ebick=new Ebick();
        ebick.setBrand(brand);
        ebick.setBuyfrom(buyfrom);
        ebick.setDetails(details);
        ebick.setImage(file.getOriginalFilename());
        String res=ebickService.ebickRegister(ebick,file,student);
        System.out.println(res);
        if(res != "success") {
            return "404";
        }

        return "redirect:/getAllMyEbicks.do";
        //return "";
    }


    @RequestMapping("scan-getOneEbick.do")
    public String sacnGetOneEbick(String ebickID,HttpSession session){
        User user = (User)session.getAttribute("user");
        Ebick ebick = ebickDao.findByID(ebickID,user.getUniversityID());
        if(ebick==null){
            return "404";
        }
        session.setAttribute("scanEbick",ebick);

        String res = drivingLicenseService.superviserFindDrivingLicense(ebickID,session);
        System.out.println(res);
        Student student = (Student)session.getAttribute("student");
        if(student!=null)
        return "redirect:/student-findEbickAndDL";
        else return "redirect:/superviser-findDrivingLicense";
    }



}
